C++ Bubble Sorting for Singly Linked List [closed]

Posted by user1119900 on Stack Overflow See other posts from Stack Overflow or by user1119900
Published on 2011-12-28T20:17:15Z Indexed on 2012/09/04 21:38 UTC
Read the original article Hit count: 166

Filed under:
|
|
|
|

I have implemented a simple word frequency program in C++. Everything but the sorting is OK, but the sorting in the following script does not work. Any emergent help will be great..


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <iostream>
#include <fstream>
#include <cstdio>
using namespace std;
#include "ProcessLines.h"

struct WordCounter {
    char *word;
    int word_count;
    struct WordCounter *pNext; // pointer to the next word counter in the list
};

/* pointer to first word counter in the list */
struct WordCounter *pStart = NULL;
/* pointer to a word counter */
struct WordCounter *pCounter = NULL;    

/* Print statistics and words */

void PrintWords() {
    ...
    pCounter = pStart;
    bubbleSort(pCounter);
    ...
} //end-PrintWords

void bubbleSort(struct WordCounter *ptr) {
    WordCounter *temp = ptr;
    WordCounter *curr;

    for (bool didSwap = true; didSwap;) {
        didSwap = false;
        for (curr = ptr; curr->pNext != NULL; curr = curr->pNext) {
            if (curr->word > curr->pNext->word) {
                temp->word = curr->word;
                curr->word = curr->pNext->word;
                curr->pNext->word = temp->word;
                didSwap = true;
            } 
        }
    }
}

© Stack Overflow or respective owner

Related posts about c++

Related posts about list